#include "Base.h"
//#include <string>
LRESULT CALLBACK RenderWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
//using namespace std;

int WINAPI WinMain(HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpCmdLine,
	int nCmdShow)
{

	WNDCLASSEX wcex;        //Clase window para definir una nueva ventana
	MSG msg;                //mensaje de ventana a decodificar en cada loop
	BOOL bQuit = FALSE;     //flag para saber si se ha cerrado la ventana

	/**Parametros que se pasan a la funcion enable opengl para indicarle en que ventana se debe dibujar **/
	HWND hwnd;
	HDC hDC;
	HGLRC hRC;
	/***************************************************************/

	//    INITCOMMONCONTROLSEX initc;
	//    initc.dwSize=sizeof(INITCOMMONCONTROLSEX);
	//    initc.dwICC= 0x00004000;
	//    InitCommonControlsEx(&initc);

#ifdef DEBUGGING_WINDOW
	float cx = 0.0;
	float cy = 0.0;
	float cz = 0.0;

	char* scx = new char[50];
	char* scy = new char[50];
	char* scz = new char[50];


	wcex.cbSize = sizeof (WNDCLASSEX);
	wcex.style = CS_DBLCLKS;                 /* Catch double-clicks */
	wcex.lpfnWndProc = InterfaceWndProc;      /* This function is called by windows */
	wcex.hInstance = hInstance;
	wcex.lpszClassName = "OGL_Int";




	/* Use default icon and mouse-pointer */
	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.lpszMenuName = NULL;                 /* No menu */
	wcex.cbClsExtra = 0;                      /* No extra bytes after the window class */
	wcex.cbWndExtra = 0;                      /* structure or the window instance */
	/* Use Windows's default colour as the background of the window */
	wcex.hbrBackground = (HBRUSH)COLOR_BACKGROUND;

	if (!RegisterClassEx(&wcex))
		return 0;

	hwndINT = CreateWindowEx(0,
		"OGL_Int",
		"Game Init",
		WS_OVERLAPPEDWINDOW,
		700,
		0,
		500,
		500,
		HWND_DESKTOP,
		NULL,
		hInstance,
		NULL);

	ShowWindow(hwndINT, nCmdShow);
#endif // DEBUGGING_WINDOW

	/* register window class */
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_OWNDC;
	wcex.lpfnWndProc = RenderWndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = "OGL_Game";
	wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	if (!RegisterClassEx(&wcex))
		return 0;

	/* create main window */
	hwnd = CreateWindowEx(0, //Extended STYLE
		"OGL_Game",
		"Game",
		0, //STYLE
		0,
		0,
		500,
		500,
		HWND_DESKTOP,
		NULL,
		hInstance,
		NULL);

	SetWindowLongPtr(hwnd, GWL_STYLE, WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX);
	ShowWindow(hwnd, nCmdShow);



	/*MStte.LB = false;
	MStte.RB = false;
	MStte.x = 0;
	MStte.y = 0;
	fns.ext_flag = false;

	for (int i = 0; i<255; i++)
	{
		KBStte.keys[i] = false;
	}





	g_c_char = new c_char();
	g_terr_list = new terr_list(g_c_char);

	g_GLwnd = new WindowPropertiesGL(&hwnd);



	g_terr_list->load_terr_headers();
	g_terr_list->terr_loader();



	//    char mn[10]="hm11.terr";
	//    TerrT = new Terrain(mn);

	_beginthread(char_thread, 0, 0);

	// enable OpenGL for the window 
	EnableOpenGL(hwnd, &hDC, &hRC);
	render_init();*/

#if defined DEBUG_WSIZE && defined DEBUGGING_WINDOW
	print("Antes de SetRes\r\n");
#endif

	//g_GLwnd->SetResolution(WP_640x480_43);
	//g_GLwnd->Set3DContext();

#if defined DEBUG_WSIZE && defined DEBUGGING_WINDOW
	print("Despues de SetRes\r\n");
#endif

	/* program main loop */
	while (!bQuit)
	{
		/* check for messages */
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			/* handle or dispatch messages */
			if (msg.message == WM_QUIT)
			{
				bQuit = TRUE;
			}
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		else
		{
#ifdef DEBUGGING_WINDOW

			if (g_c_char->Get(CHAR_X) != cx || g_c_char->Get(CHAR_Y) != cy || g_c_char->Get(CHAR_Z) != cz)
			{

				cx = g_c_char->Get(CHAR_X);
				cy = g_c_char->Get(CHAR_Y);
				cz = g_c_char->Get(CHAR_Z);

				sprintf(scx, "%5.3f", cx);
				sprintf(scy, "%5.3f", cy);
				sprintf(scz, "%5.3f", cz);


				SetDlgItemText(hwndINT, ID_CHARX, (LPCSTR)scx);
				SetDlgItemText(hwndINT, ID_CHARY, (LPCSTR)scy);
				SetDlgItemText(hwndINT, ID_CHARZ, (LPCSTR)scz);
			}
#endif // DEBUGGING_WINDOW

			//KeyPresses();
			//display();
		//	SwapBuffers(hDC);
			//Sleep(5);
		}
	}

	/* shutdown OpenGL */
	//DisableOpenGL(hwnd, hDC, hRC);
	/* destroy the window explicitly */
	DestroyWindow(hwnd);

	return msg.wParam;
}





LRESULT CALLBACK RenderWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	//    static HINSTANCE hInstance;
	//    static HWND hctrl;
	//static int lmouse_x;
	//static int lmouse_y;

	switch (uMsg)
	{

	case WM_CREATE:

		break;



	//case WM_MOUSEWHEEL:

	//	//(int)(0x0000FFFF&lParam);
	//	MStte.wheel += (((int)(0xFFFF0000 & wParam)) >> 16) / 120;

	//	break;
	//case WM_MOUSEMOVE:
	//	lmouse_x = (int)(0x0000FFFF & lParam);
	//	lmouse_y = ((int)(0xFFFF0000 & lParam)) >> 16;

	//	MStte.dx = lmouse_x - MStte.x;
	//	MStte.dy = lmouse_y - MStte.y;

	//	MStte.x = lmouse_x;
	//	MStte.y = lmouse_y;
	//	break;
	//case  WM_LBUTTONDOWN:
	//	MStte.LB = true;
	//	break;
	//case  WM_LBUTTONUP:
	//	MStte.LB = false;
	//	break;
	//case  WM_RBUTTONDOWN:
	//	MStte.RB = true;
	//	break;
	//case  WM_RBUTTONUP:
	//	MStte.RB = false;
	//	break;




	//case WM_KEYUP:
	//	KBStte.keys[(unsigned char)wParam] = false;
	//	break;
	//case WM_KEYDOWN:
	//	KBStte.keys[(unsigned char)wParam] = true;
	//	//            switch (wParam)
	//	//            {
	//	//                case VK_ESCAPE:
	//	//                    PostQuitMessage(0);
	//	//                    break;
	//	//
	//	//                break;
	//	//            }
	//	break;

	case WM_CLOSE:
		PostQuitMessage(0);
		break;

	case WM_DESTROY:
		return 0;


	default:
		return DefWindowProc(hwnd, uMsg, wParam, lParam);
		break;
	}

	return 0;
}